﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Snake.Core;
using Snake.Mapping;
using Snake.SQL;
using Snake.Test.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Snake.Test
{
    [TestClass]
    public class GeneratedSqlTest
    {


      
        [TestMethod]
        public void EntityRelationAccessInWhere()
        {
            var con = @"Data Source=.\sql2008;Initial Catalog=Snake;Persist Security Info=True;User ID=sa;Password=sa";

            var factory = new Factory(con);
            var session = (Session) factory.Create();
            //var Employee = session.Get<Parent>(1);
            //var c0 = session.GetAll<Employee>().Where(a => a.Department.Work.Name.Contains("Q"));
            //session.Delete<Employee>(a => a.Department.Work.Name.Contains("志东"));
            //var c1 = session.GetAll<Employee>().Where(a => a.Name.Contains("Q")).ToList();

            //var context = AssertHelper.CreateContext(session, c0);
            //var sql= context.Writer.GetResult();


            //var fuck = query.GetAll<Parent>().Where(a => a.Name.Contains("志东"));
            //var fuckcontext = AssertHelper.CreateContext(query, fuck).Writer.GetResult();

            //var stus = query.GetAll<Employee>()
            //                .Where(a => a.Parent.Name.Contains("EF"));

            //var context = AssertHelper.CreateContext(query, stus);


            //var result = context.Writer.GetResult();

            //Assert.AreEqual("%EF%", context.Values["@p0"].Value);
        }

        [TestMethod]
        public void ReferenceNullInWhere()
        {
            var factory = new Factory("");
            var query = (Session) factory.Create();
            var courses = query.GetAll<Employee>()
                               .Where(a => a.Name.Contains("Yubq"));

            var context = AssertHelper.CreateContext(query, courses);
            var result = context.Writer.GetResult();

        
        }

        [TestMethod]
        public void ManyParameters()
        {
            var factory = new Factory("");
            var query = (Session) factory.Create();
            var stus = query.GetAll<Employee>()
                            .Where(a => a.ID == 1 && a.ID == 1 && a.ID == 1 && a.ID == 1 && a.ID == 1 && a.ID == 1 && a.ID == 1);

            var context = AssertHelper.CreateContext(query, stus);

            Assert.AreEqual(7, context.Values.Count);
        }

        [TestMethod]
        public void TestOrderBy()
        {
            var factory = new Factory("");
            var query = (Session) factory.Create();
            var stus = query.GetAll<Employee>()
                            .Where(a=>a.Name.Contains("zhidong"))
                            .OrderBy(a => a.Name)
                            .ThenByDescending(a => a.Name);
            var context = AssertHelper.CreateContext(query, stus);
            var result = context.Writer.GetResult();
           
        }

        [TestMethod]
        public void OrderByReference()
        {
            var factory = new Factory("");
            var query = (Session) factory.Create();
            //var stus = query.GetAll<Employee>()
            //    .OrderBy(a=>a.Parent.ID);

            var stus = query.GetAll<Employee>()
                .Where(a => a.Name.Contains("Yubq"));
            var context = AssertHelper.CreateContext(query, stus);
            var result = context.Writer.GetResult();
           
        }

        [TestMethod]
        public void TestTake()
        {
            var factory = new Factory("");
            var query = (Session) factory.Create();
            var stus = query.GetAll<Employee>()
                            .Take(10);
            var context = AssertHelper.CreateContext(query, stus);
            var result = context.Writer.GetResult();
            
        }

        [TestMethod]
        public void TestSkip()
        {
            var factory = new Factory("");
            var query = (Session) factory.Create();
            var stus = query.GetAll<Employee>().Where(a => a.Name.Contains("BQ"))
                                            .OrderBy(_ => _.Name)
                                            .GroupBy(a => a.Name)
                                            .Skip(10);
            var context = AssertHelper.CreateContext(query, stus);
            var result = context.Writer.GetResult();
          
        }

        [TestMethod]
        public void TestSkipWithoutOrder()
        {
            var factory = new Factory("");
            var query = (Session) factory.Create();
            var stus = query.GetAll<Employee>()
                            .Skip(10)
                            .Take(10);
            var context = AssertHelper.CreateContext(query, stus);
            var result = context.Writer.GetResult();
           
        }
        [TestMethod]
        public void TestDelete()
        {
            var factory = new Factory("");
            var session = (Session) factory.Create();
           // session.Delete<Student>(2);
           var test= session.GetAll<Employee>().Where(a => a.Name.Contains("Yubq"));

            var employee = new Employee();
            employee.ID = 5;
            employee.Name = "志东妹子";
            employee.Department_ID = 2;

            session.Delete<Employee>(new string[] { "fuck1", "fuck2", "fuck3", "fuck4" });
            session.Delete(employee);
            session.Delete<Employee>(5);
           


            var ids = new int[] {5, 5, 6, 7};
           
            // session.GetAll<Employee>().Where();
            //session.Delete<Employee>(a => a.Name.Contains("YuBQ") && a.Name.Contains("zd") && a.ID > 10);

        }

        [TestMethod]
        public void TestSava()
        {
            var con = @"Data Source=.\sql2008;Initial Catalog=Snake;Persist Security Info=True;User ID=sa;Password=sa";

            var factory = new Factory("");
            var session = (Session) factory.Create();
            var w1=  session.Get<Work>(1);
            var w2=  session.Get<Work>(1);
            //var employee = new Employee();
            //employee.ID = 5;
            //employee.Name = "青菜妹子";
            //employee.Department_ID = 2;
            //session.Save(employee);

            var work = new Work();
            work.Name = "抢银行";
            session.Save(work);
        }

        public class TestStringKey
        {
            public string ID { get; set; }
            public string Name { get; set; }
        }

        [TestMethod]
        public void TestUpdate()
        {
            var factory = new Factory("");
            var session = factory.Create();
            var stringkey = new TestStringKey();
            stringkey.ID = "妹子";
            stringkey.Name = "菜";
            session.Save(stringkey);
            //var employee = new Employee();
            //employee.ID = 5;
            //employee.Name = "青菜妹子";
            //employee.Department_ID = 2;
            //session.Update(employee);

        }
    }
}
